import requests
import json

# 配置测试参数
BASE_URL = "http://192.168.1.99:5000/api"
LOGIN_URL = f"{BASE_URL}/auth/login"  # 假设登录接口路径为/auth/login

# 测试用户凭证
USERNAME = "admin"
PASSWORD = "admin123"

def get_jwt_token():
    """获取有效的JWT令牌"""
    # 准备请求数据
    login_data = {
        "username": USERNAME,
        "password": PASSWORD
    }
    
    # 发送登录请求
    print(f"发送登录请求到: {LOGIN_URL}")
    print(f"请求数据: {json.dumps(login_data, ensure_ascii=False)}")
    
    try:
        response = requests.post(LOGIN_URL, json=login_data)
        
        print(f"响应状态码: {response.status_code}")
        
        if response.status_code == 200:
            try:
                response_data = response.json()
                print(f"响应内容: {json.dumps(response_data, ensure_ascii=False, indent=2)}")
                
                if 'access_token' in response_data:
                    return response_data['access_token']
                else:
                    print("响应中未找到access_token")
                    return None
            except json.JSONDecodeError:
                print(f"响应内容不是有效的JSON: {response.text}")
                return None
        else:
            try:
                print(f"响应内容: {json.dumps(response.json(), ensure_ascii=False, indent=2)}")
            except json.JSONDecodeError:
                print(f"响应内容: {response.text}")
            return None
    except Exception as e:
        print(f"请求过程中发生错误: {str(e)}")
        return None

if __name__ == "__main__":
    token = get_jwt_token()
    if token:
        print(f"\n获取到的JWT令牌: {token}")
    else:
        print("\n未能获取到有效的JWT令牌")